package cn.sytton.taffe.web.sys.controller;

import cn.sytton.taffe.web.sys.entity.Auth;
import cn.sytton.taffe.web.sys.service.IPositionMenuService;
import com.github.pagehelper.PageInfo;
import cn.sytton.taffe.framework.base.controller.BaseController;
import cn.sytton.taffe.framework.base.entity.PageParam;
import cn.sytton.taffe.excel.annotation.ExportExcel;
import cn.sytton.taffe.web.sys.entity.PositionEntity;
import cn.sytton.taffe.web.sys.service.IPositionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 职位岗位控制层
 *
 * @author skyrock
 */
@Tag(name = "职位岗位")
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/position")
public class PositionController extends BaseController {

    private final IPositionService positionService;
    private final IPositionMenuService positionMenuService;

    @Operation(summary = "查询分页职位岗位列表")
    @Parameter(name = "pp", description = "分页参数对象", in = ParameterIn.QUERY, required = true)
    @ExportExcel
    @GetMapping
    public PageInfo<PositionEntity> findPage(PageParam pp) {
        return positionService.findPage(pp);
    }

    @Operation(summary = "查询单个职位岗位")
    @Parameter(name = "id", description = "职位岗位ID", in = ParameterIn.PATH, required = true)
    @GetMapping("/{id}")
    public PositionEntity findById(@PathVariable String id) {
        return positionService.findById(id);
    }

    @Operation(summary = "查询多个岗位", description = "body: 主键ID数组")
    @PostMapping("/ids")
    public List<PositionEntity> findByIds(@RequestBody String[] ids) {
        return positionService.listByIds(Arrays.asList(ids));
    }

    @Operation(summary = "保存职位岗位")
    @PostMapping
    public void save(@RequestBody @Validated PositionEntity entity) {
        positionService.createOrUpdate(entity);
    }

    @Operation(summary = "删除职位岗位", description = "body: 主键ID数组")
    @PostMapping("/delete")
    public void delete(@RequestBody String[] ids) {
        positionService.delete(ids);
    }

    @Operation(summary = "查询职位岗位列表")
    @GetMapping("/list")
    public List<PositionEntity> findList() {
        return positionService.findList();
    }

    @Operation(summary = "获取职位的授权菜单ID及功能ID")
    @Parameter(name = "positionId", description = "职位ID", in = ParameterIn.QUERY, required = true)
    @GetMapping("/menu")
    public Map<String, Object> findMenu(@RequestParam String positionId) {
        return positionMenuService.findMenu(positionId);
    }

    @Operation(summary = "授权菜单及权限")
    @PostMapping("/menu/auth")
    public void authMenu(@RequestBody Auth auth) {
        positionMenuService.authMenu(auth);
    }
}
